package com.example.security.controller;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/hello")
    public String hell(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (principal instanceof UserDetails){
            username = ((UserDetails) principal).getUsername();
            System.out.println(username);
        }else {
            username = principal.toString();
            System.out.println(username);
        }
        return "hello word" + username;
    }

    @GetMapping("/admin")
    public String admin(){
        return "hello admin";
    }
}
